package com.angel.web.health;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.angel.core.util.JsonUtil;
import com.angel.core.util.Pager;
import com.angel.health.BuzCustomer;
import com.angel.health.BuzExaminationParsingWithBLOBs;
import com.angel.provider.health.BuzCustomerProvider;
import com.angel.provider.health.BuzExaminationParsingProvider;
import com.github.pagehelper.StringUtil;

import net.sf.json.util.JSONUtils;

/** 
* @author 作者姓名  cheng
* @version 
* 创建时间：2016年11月23日 下午5:27:30 
* 类说明 
*/
@Controller
@RequestMapping("/archives")
public class BuzArchivesController {
	
	@Autowired
	private BuzExaminationParsingProvider bepProvider;
	@Autowired
	private BuzCustomerProvider bcProvider;

	@RequestMapping("/index")
	public String index(BuzCustomer bc,Pager pager,HttpServletRequest request){
		Pager bean = bcProvider.selectCustomerByBep(bc,pager);
		request.setAttribute("pager", bean);
		return "archives/archives";
	}
	
	@RequestMapping("look")
	public String look(BuzExaminationParsingWithBLOBs bep,Pager pager,HttpServletRequest request){
		Pager bean = bepProvider.findAllForPage(bep,pager);
		Map<String,Map<String,String>> mapValue = new LinkedHashMap<String,Map<String,String>>();
		
		List<Map<String,Object>> list = bean.getList();
		String medical = "";
		if(list.size() > 0){
			medical = list.get(0).get("medical").toString();
			Map<String, Object> map = JsonUtil.jsonToMap(medical);
			for (Entry<String, Object> entry : map.entrySet()) {
				String key = entry.getKey();
				String value = entry.getValue().toString();
				if("{}".equals(value) || StringUtil.isEmpty(value)){
					continue;
				}
				Map<String,String> _mapVlue = new HashMap<String,String>();
				Map<String, Object> _map = JsonUtil.jsonToMap(value);
				for(Entry<String, Object> _entry : _map.entrySet()){
					String _key = _entry.getKey();
					String _value = _entry.getValue().toString();
					if(StringUtil.isEmpty(_value) || "null".equals(_value) || "\"null\"".equals(_value)){
						continue;
					}
					_key = "tjsj".equals(_key) ? "体检所见" : _key;
					_mapVlue.put(_key, _value);
				}
				mapValue.put(key, _mapVlue);
			}
		}
		System.err.println(JsonUtil.toJson(mapValue));
		request.setAttribute("mapBean", mapValue);
		request.setAttribute("pager", bean);
		return "archives/lookArchives";
	}
}
